from fields.area_fields_def import area_fields
from libs.login import login_required
from services.area_init_service import AreaService
from flask_restful import Resource, marshal_with
from controllers.common import api
from flask import request


class AreaApi(Resource):

    @marshal_with(area_fields)
    @login_required
    def get(self):
        """获取区域列表"""
        parent_code = request.args.get("parent_code", None, str)
        area_type = request.args.get("area_type", 1, int)
        if parent_code is None:
            return []
        return AreaService.get_areas_by_parent(parent_code, area_type)


api.add_resource(AreaApi, "/area")
